<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
    append-to-body>
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="90px">
      <el-row>
        <el-col :span="10">
          <el-form-item label="专栏" prop="type">
            <el-select v-model="dataForm.type" filterable placeholder="请选择分类" size="medium" @change="onSelectType">
              <el-option v-for="item in aibumTypeList" :key="item.val" :label="item.name" :value="item.val"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="10">
          <el-form-item label="专栏名称" prop="name">
            <el-input v-model="dataForm.name" placeholder="专栏名称"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="专栏封面:" prop="image">
            <div class="myhead">
              <el-tooltip content="封面图" placement="top">
                <el-image  style="width: 90px; height: 60px" :src="dataForm.image + '?x-oss-process=image/resize,h_100,w_100'" fit="contain" @click="uploadImageHandle()" alt=""/>
              </el-tooltip>
            </div>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
    <!-- 弹窗, 上传组件图 -->
    <album-upload-image v-if="uploadImageVisible" ref="albumUploadImage" @refreshDataList="refreshUploadImage"></album-upload-image>

  </el-dialog>
</template>

<script>
  import AlbumUploadImage from './album-upload-image'
  export default {
    data () {
      return {
        visible: false,
        uploadImageVisible: false,
        dataForm: {
          id: 0,
          type: '',
          name: '',
          sort: '',
          classify:'2',
          image: 'http://file-cdn-theme.cykonhy.com/default/default.jpg',
        },
        dataRule: {
          type: [
            { required: true, message: '分类不能为空', trigger: 'blur' }
          ],
          name: [
            { required: true, message: '专题名称不能为空', trigger: 'blur' }
          ],
          image: [
            { required: true, message: '专题图片不能为空', trigger: 'blur' }
          ]
        },
        aibumTypeList:[
          {
            "id": 1,
            "val": 1,
            "name": "主题",
          }, {
            "id": 2,
            "val": 2,
            "name": "组件",
          }, {
            "id": 3,
            "val": 3,
            "name": "壁纸",
          }
        ],
      }
    },
    components: {
      AlbumUploadImage
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/sys/recommend/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                debugger
                this.dataForm.type = data.recommend.type
                this.dataForm.name = data.recommend.name
                this.dataForm.image = data.recommend.image
                this.dataForm.sort = data.recommend.sort
                this.dataForm.classify = data.recommend.classify
              }
            })
          }
        })
      },
      // 筛选尺寸
      onSelectType (val) {
        let typeItem = null ;
        this.aibumTypeList.forEach((item, index) => {
          if (item.val === val){
            typeItem = item
          }
        })
      },

      // 上传组件图
      uploadImageHandle () {
        this.uploadImageVisible = true
        this.$nextTick(() => {
          this.$refs.albumUploadImage.init()
        })
      },
      refreshUploadImage (filePath) {
        if (filePath != ""){
          this.dataForm.image = filePath
        }
        this.uploadImageVisible = false
      },

      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/sys/recommend/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'type': this.dataForm.type,
                'name': this.dataForm.name,
                'image': this.dataForm.image,
                'sort': this.dataForm.sort,
                'classify': this.dataForm.classify,
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
